import functools
import inspect
from typing import Any, Callable, TypeVar, cast

F = TypeVar('F', bound=Callable[..., Any])

def type_check(func: F) -> F:
    """运行时类型检查装饰器"""
    sig = inspect.signature(func)
    
    @functools.wraps(func)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        bound = sig.bind(*args, **kwargs)
        bound.apply_defaults()
        
        for param_name, param_value in bound.arguments.items():
            param = sig.parameters[param_name]
            if param.annotation is not inspect.Parameter.empty:
                if not isinstance(param_value, param.annotation):
                    raise TypeError(
                        f"参数 '{param_name}' 必须是 {param.annotation.__name__} 类型, "
                        f"但收到了 {type(param_value).__name__} 类型"
                    )
        
        result = func(*args, **kwargs)
        
        if sig.return_annotation is not inspect.Signature.empty:
            if not isinstance(result, sig.return_annotation):
                raise TypeError(
                    f"返回值必须是 {sig.return_annotation.__name__} 类型, "
                    f"但收到了 {type(result).__name__} 类型"
                )
        
        return result
    
    return cast(F, wrapper)

# 使用装饰器
@type_check
def add(a: int, b: int) -> int:
    return a + b

# 这会正常工作
result = add(1, 2)  # 返回 3

# 这会在运行时引发TypeError
# result = add("1", 2)  # 引发TypeError: 参数 'a' 必须是 int 类型, 但收到了 str 类型